package com.example.lanouhn.firstapp.activity.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.lanouhn.firstapp.R;
import com.example.lanouhn.firstapp.activity.adapter.CollectionStoreAdapter;
import com.example.lanouhn.firstapp.activity.model.CollectionStore;
import com.example.lanouhn.firstapp.activity.utils.CollectionStoreJson;
import com.example.lanouhn.firstapp.activity.utils.HttpUtils;
import com.example.lanouhn.firstapp.activity.utils.SPUtils;

import java.util.List;

//我的收藏店铺页面
public class CollectionStoreActivity extends AppCompatActivity {
    //关于我们--跳转
    private TextView tv_aboutOur;
    //免责声明
    private TextView tv_disclaimer;
    //商务合作
    private TextView tv_cooperate;
    //帮助中心
    private TextView tv_help;
    //注销
    private TextView tv_cancellation;
    //返回
    private ImageView iv_back;
    private TextView mineCenter;


    //记录
    private TextView noRecord;

    //定义key
    private String key;
    //数据集合
    private List<CollectionStore> list;
    private CollectionStoreAdapter adapter;
    private RecyclerView recyclerView;

    private MyBroadcastReceiver myBroadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_bookmark);
        //获取key
        key = (String) SPUtils.get(CollectionStoreActivity.this, "log_key", "");
        //绑定控件
        initView();
        //点击事件
        initEvent();
        //获取网络数据
        getDataFromWeb();

    }

    private void getDataFromWeb() {

        noRecord.setVisibility(View.GONE);
        new Thread(new Runnable() {
            @Override
            public void run() {
                String url = "http://www.niaolema.com/mobile/index.php?act=member_favorites_store&op=favorites_list";
                String params = "key=" + key;
                String result = HttpUtils.doPost(url, params);
                if (!result.equals("") && !result.equals("null")) {
                    list = CollectionStoreJson.getCollectionStore(result);

                    if (list.size() > 0) {

                        handler.sendEmptyMessage(1);

                    }else {
                        handler.sendEmptyMessage(0);
                    }
                }else {
                    handler.sendEmptyMessage(0);
                }


            }
        }).start();


    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case 0:
                    noRecord.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    adapter = new CollectionStoreAdapter(list,CollectionStoreActivity.this);
                    recyclerView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();

                    //点击跳转到商城页面
                    adapter.setCollectionStoreRecyclerViewListener(new CollectionStoreAdapter.CollectionStoreRecyclerViewListener() {
                        @Override
                        public void onClick(View view, int position) {
                            Intent intent = new Intent(CollectionStoreActivity.this,BrokerDealerActivity.class);
                            startActivity(intent);
                        }
                    });

                    break;
                default:
                    break;


            }
        }
    };

    //定义广播接收器
    class MyBroadcastReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            getDataFromWeb();
        }
    }
    private void initView() {
        mineCenter = (TextView) findViewById(R.id.mineCenter);
        iv_back = (ImageView) findViewById(R.id.iv_back);

        noRecord = (TextView) findViewById(R.id.noRecord);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        //加载局管理器
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 1);
        recyclerView.setLayoutManager(gridLayoutManager);

        //动态注册广播接收器
        myBroadcastReceiver = new MyBroadcastReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("delete");
        registerReceiver(myBroadcastReceiver,filter);


        //绑定控件
        tv_aboutOur = (TextView) findViewById(R.id.mine_aboutOur);
        tv_disclaimer = (TextView) findViewById(R.id.mine_disclaimer);
        tv_cooperate = (TextView) findViewById(R.id.mine_cooperate);
        tv_help = (TextView) findViewById(R.id.mine_help);
        tv_cancellation = (TextView) findViewById(R.id.mine_cancellation);


    }

    private void initEvent() {

        //关于我们--的点击事件
        tv_aboutOur.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CollectionStoreActivity.this, AboutOurActivity.class);
                startActivity(intent);
            }
        });

        //免责声明--点击事件
        tv_disclaimer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CollectionStoreActivity.this, DisclaimerActivity.class);
                startActivity(intent);
            }
        });
        //商务合作--点击事件
        tv_cooperate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CollectionStoreActivity.this, CooperateActivity.class);
                startActivity(intent);
            }
        });
        //帮助中心--点击事件
        tv_help.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CollectionStoreActivity.this, HelpActivity.class);
                startActivity(intent);
            }
        });

        //注销--点击事件
        tv_cancellation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(CollectionStoreActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });
        //个人中心--点击事件
        mineCenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //返回
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });



    }

    //注销
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(myBroadcastReceiver);
    }
}